package com.study.services;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jmx.export.assembler.AbstractConfigurableMBeanInfoAssembler;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.IAnniversaryDAO;
import com.study.model.jdao.ICustomerDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.mdao.AnniversaryMapper;
import com.study.model.mdao.LogforcustDAO;
import com.study.model.pojo.Anniversary;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;

@Service
@Transactional(rollbackOn = Exception.class)
public class AnniversaryService {
	
	@Autowired
	IAnniversaryDAO dao;
	@Autowired
	AnniversaryMapper mapper;
	@Autowired
	IDictionaryDAO iDao;
	@Autowired
	ICustomerDAO customerDAO;
	@Autowired
	ILinkmanDAO linkmanDAO;
	@Autowired
	LogforcustDAO logforcustDAO;
	
	/**
	 * 根据id查询
	 * @param annId
	 * @return
	 */

	public JSONObject selectById(String ann_id) {
		return mapper.selectById(ann_id);
	}
	
	/**
	 * 查询所有
	 * @return
	 */
	public List<JSONObject> findAll11() {
		return mapper.selectAll();
	}
	
	/**
	 * 新增记录
	 * @param anniversary
	 */
		public void insertAnn(Anniversary anniversary){
			dao.save(anniversary);
			 Linkman linkman = anniversary.getLinkman();
			 int dicId = anniversary.getDictionary().getDicId();
			 String meg = null;
			 if (dicId == 2111) {
				meg ="&nbsp;&nbsp;公司周年庆纪念日";
			}else if (dicId == 2112) {
				meg="&nbsp;&nbsp;生日";
			}
			Customer customer = linkman.getCustomer();
			 int useId = customer.getUser().getUseId();
			 String logMessage = "新增了"+linkman.getLinName()+meg;
			logforcustDAO.insert(customer.getCusId(), new Date(), useId, 2202, logMessage, "");
		}
	
		/**
		 * 根据编号删除记录
		 * @param annId
		 */
	public void delete(int annId) {
		Anniversary an = dao.findByAnnId(annId);
		an.setAdel(0);
	}
	
	
	/**
	 * 带条件的查询
	 * 
	 * @param pageNum
	 * @param pageNum
	 * @param input
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects,Integer use_id) {
		String ann = null;
		PageHelper.startPage(pageNum, pageSize);
		if (selects.length()>0) {
			ann = selects;
		} 
		return mapper.selectByWhere(ann, input,use_id);
	}


	/**
	 * 编辑纪念日
	 * @param cus_id
	 * @param lin_id
	 * @param annity
	 * @param date1
	 * @param remark
	 * @param remind
	 * @param id
	 * @return
	 */
	public boolean editanni(Integer custid,Integer linId,
			Integer annity,
			Date date1,
			String remark,
			String remind,String id) {
		Anniversary a = dao.findByAnnId(Integer.parseInt(id));
		a.setCustomer(customerDAO.findByCusId(custid));
		a.setAnnDate(date1);
		a.setDictionary(iDao.findByDicId(annity));
		a.setAnnRemark(remark);
		a.setLinkman(linkmanDAO.findByLinId(linId));
		return true;
	}

}
